class data plot

dat2 = read.csv("class.txt", header = TRUE, sep = "")
library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
plot.year = ggplot(dat2) + geom_bar(aes(x = Year))
ggplotly(plot.year)
plot.program = ggplot(dat2) + geom_bar(aes(x = Program))
ggplotly(plot.program)

health spending by States plot (Y in log form)

# Read data, excluding title and notes
dat4 = head(read.csv("spending.csv", skip = 2, sep = ","), -9)
# Transform from wide format to long format
dat4.long = reshape(dat4, varying = 2:25, 
                    v.names = "spending", 
                    timevar = "time", 
                    direction = "long")
# Label the real year
dat4.long$time = dat4.long$time + 1990

plot.spending = ggplot(dat4.long, aes(time, spending, colour = Location)) + 
  geom_line(size = 0.1) + 
  geom_point(size = 0.3) + 
  xlab("Year") + 
  ylab("Health care spending in log form") + 
  scale_y_continuous(trans = "log10")
ggplotly(plot.spending)

Average health care spending by States (Y in log form)

# Get means
avg = rowMeans(dat4 %>% select(-Location))
# combine State name and mean spending of that state
avg.spending = cbind(dat4 %>% select(Location), avg)
# Match state name with abbreviated state name to show in the plot
avg.spending$Acronyms = state.abb[match(avg.spending$Location, state.name)]
# District of Columbia is coded as "DC", United States is coded "US"
avg.spending$Acronyms[avg.spending$Location == "District of Columbia"] = "DC"
avg.spending$Acronyms[avg.spending$Location == "United States"] = "US"
#Bar plot
plot.avg = ggplot(avg.spending[!avg.spending$Acronyms == "US",], aes(Acronyms, avg)) + 
  geom_col() + 
  scale_y_continuous(trans = "log10") + 
  geom_col(data = avg.spending[avg.spending$Acronyms == "US",], aes(Acronyms, avg), fill = "red") +
  xlab("States") + 
  ylab("Average health care spending")
ggplotly(plot.avg)